/**
 * jims
 */
package com.jims.master.modules.blood.web;

import com.jims.master.common.data.StringData;
import com.jims.master.common.utils.StringUtils;
import com.jims.master.common.web.BaseController;
import com.jims.master.common.web.impl.BaseDto;
import com.jims.master.modules.blood.entity.BloodCapacity;
import com.jims.master.modules.blood.entity.BloodMatch;
import com.jims.master.modules.blood.entity.BloodOutSheet;
import com.jims.master.modules.blood.service.BloodMatchService;
import com.jims.master.modules.blood.service.BloodOutSheetService;
import com.jims.master.modules.blood.vo.BloodMatchOutSheetVo;
import com.jims.master.modules.sys.entity.User;
import com.jims.master.modules.sys.utils.UserUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

/**
 * 发血单Controller
 * @author CTQ
 * @version 2017-04-13
 */
@Controller
@RequestMapping(value = "${adminPath}/blood/bloodOutSheet")
public class BloodOutSheetController extends BaseController {

	@Autowired
	private BloodOutSheetService bloodOutSheetService;
	@Autowired
	private BloodMatchService bloodMatchService;

	@ModelAttribute
	public BloodOutSheet get(@RequestParam(required=false) String id) {
		BloodOutSheet entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = bloodOutSheetService.get(id);
		}
		if (entity == null){
			entity = new BloodOutSheet();
		}
		return entity;
	}

    /**
     * 发血首页
     * @param bloodOutSheet
     * @param model
     * @return
     * @ CTQ
     */
	@RequiresPermissions("user")
	@RequestMapping(value = {"index", ""})
	public String list(BloodOutSheet bloodOutSheet, Model model) {
		List<User> users= UserUtils.getRoleUserList(UserUtils.getUser().getOrgDept()!=null?UserUtils.getUser().getOrgDept().getId():"");
		model.addAttribute("users",users);
		if (bloodOutSheet.getStartDate()==null){
			bloodOutSheet.setStartDate(new Date());
		}
		if (bloodOutSheet.getStopDate() == null) {
			bloodOutSheet.setStopDate(new Date());
		}
		BloodMatch bm = new BloodMatch();
		bm.setStartDate(bloodOutSheet.getStartDate());
		bm.setStopDate(bloodOutSheet.getStopDate());
		bm.updateOrgId();
		List<BloodMatchOutSheetVo> list = bloodMatchService.getApplyNum(bm,"1");
		model.addAttribute("list", list);
		model.addAttribute("bloodOutSheet", bloodOutSheet);
		return "modules/blood/bloodOutSheetIndex";
	}

	@RequiresPermissions("user")
	@RequestMapping(value = "applyListData")
	public String applyListData(BloodOutSheet bloodOutSheet,String applyNum ,Model model) {
		List<BloodCapacity> list = bloodMatchService.getBloodRequireByApplyNum(applyNum,"1");
		model.addAttribute("list", list);
		model.addAttribute("bloodOutSheet", bloodOutSheet);
		return "modules/blood/bloodOutApplyList";
	}

	@RequiresPermissions("user")
	@RequestMapping(value = "matchListData")
	public String matchListData(BloodOutSheet bloodOutSheet,String applyNum,Model model) {
		List<BaseDto> list = bloodMatchService.findByApplyNum(applyNum);
		model.addAttribute("list", list);
		model.addAttribute("bloodOutSheet", bloodOutSheet);
		return "modules/blood/bloodOutMatchList";
	}

	@RequiresPermissions("user")
	@RequestMapping(value = "waitOutSheetData")
	@ResponseBody
	public BaseDto waitOutSheetData(BloodOutSheet bloodOutSheet) {
		bloodOutSheet.updateOrgId();
		BaseDto baseDto = bloodOutSheetService.findWaitOutSheet(bloodOutSheet);
		return baseDto;
	}

    /**
     * 保存发血
     * @param bloodOutSheet
     * @return
     * @ CTQ
     */
	@RequiresPermissions("user")
	@RequestMapping(value = "save")
	@ResponseBody
	public StringData save(BloodOutSheet bloodOutSheet) {
	    StringData data=new StringData();
		int i=bloodOutSheetService.saveSheet(bloodOutSheet);
        if(i>0){
            data.setCode("success");
            data.setData("保存发血单成功");
        }else{
            data.setCode("error");
            data.setData("保存发血单失败");
        }
        return data;
	}

    /**
     * 发血单查询
     * @param bloodOutSheet
     * @param request
     * @param response
     * @param model
     * @return
     * @author fengyg
     */
    @RequiresPermissions("user")
    @RequestMapping(value = {"bloodOutSheetQuery",""})
    public String getBloodOutSheetList(BloodOutSheet bloodOutSheet,HttpServletRequest request,HttpServletResponse response,Model model){
        List<BloodOutSheet> list = bloodOutSheetService.getBloodOutSheetList(bloodOutSheet);
        model.addAttribute("list",list);
        return "modules/blood/bloodOutSheetQuery";
    }
}